﻿// --------------------------------------------------------
// <copyright file="BootstrapTableController.cs" company="ChiakiYu">
//     Copyright (c) 2015-2017 ChiakiYu.All rights reserved
// </copyright >
// <creation-date>2017-03-29 13:46</creation-date>
// <last-editor>于琦(yu_199012@qq.com)</last-editor>
// <last-date>2017-03-29 16:37</last-date>
// --------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web.Mvc;
using MvcDemo.Models;

namespace MvcDemo.Controllers
{
    public class BootstrapTableController : Controller
    {
        public ActionResult BootstrapTable()
        {
            return View();
        }

        public JsonResult GetData(AreaQueryForBootstrapTable query)
        {
            var data = new Area().GetData();
            if (!string.IsNullOrEmpty(query.Name))
                data = data.Where(n => n.Name.Contains(query.Name));
            if (!string.IsNullOrEmpty(query.Description))
                data = data.Where(n => n.Description.Contains(query.Description));
            if (query.X > 0)
                data = data.Where(n => Equals(n.PointX, query.X));
            if (query.Y > 0)
                data = data.Where(n => Equals(n.PointY, query.Y));

            data = !string.IsNullOrEmpty(query.Sort)
                ? data.OrderBy($"{query.Sort} {query.Order}")
                : data.OrderBy("Id " + query.Order);

            var count = data.Count();
            var result = data.Skip(query.Offset).Take(query.Limit).ToList();
            var result1 = new {total = count, rows = result};
            return Json(result1, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult Delete(IEnumerable<int> ids)
        {
            return Json("删除成功");
        }
    }
}